package com.bbx.taxi.client.Util;

import android.content.Context;

import com.bbx.api.sdk.model.official.passanger.Return.LocalCity;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.taxi.client.Bean.City;
import com.bbx.taxi.client.Bean.Line.CitiesBean.AppCitysBean;
import com.bbx.taxi.client.Bean.Line.CitiesBean.AppCitysBean.ChildCitysBeanX;
import com.bbx.taxi.client.Bean.Line.CitiesBean.AppCitysBean.ChildCitysBeanX.ToCitiesBean;
import com.bbx.taxi.client.Bean.Line.CitiesBean.AppCitysBean.ChildCitysBeanX.ToCitiesBean.ChildCitysBean;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.DB.LineDB;
import com.bbx.taxi.client.MyApplication;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：严旭文 on 2016/12/21 17:47
 * 邮箱：420255048@qq.com
 */
public class ToCity2 {
    /**全部的出发地父类*/
	public static List<AppCitysBean> list_startcity_cj=new ArrayList<>();
    /**全部的市内出发地父类*/
	public static List<AppCitysBean> list_startcity_sn=new ArrayList<>();
    /**本地城市JSON*/
    private final static String fileName = "city.json";
    public static String split[] =new String[]{ "市","区","县"};
    /**
     * 获取出发地父类集合，
     */
    public static List<AppCitysBean> getStartCityList(final boolean isSn){
        if(isSn){
           if(list_startcity_sn!=null&&!list_startcity_sn.isEmpty())
               return list_startcity_sn;
        }else{
            if(list_startcity_cj!=null&&!list_startcity_cj.isEmpty())
                return list_startcity_cj;
        }
        final LineDB db_line = new LineDB();
        List<AppCitysBean> startcity = new ArrayList<AppCitysBean>();
        startcity.addAll(db_line.readStartDB(isSn));
        startcity=filterEndNoShow(startcity);
        startcity=filterNoEnd(startcity);
        startcity=filterNoStartChildCity(startcity);
        if(isSn){
            list_startcity_sn=startcity;
        }else{
            list_startcity_cj=startcity;
        }
        return startcity;
    }
    /**
     * 获取出发地子类集合，如 厦门的 思明区，
     */
    public static List<ChildCitysBeanX> getStartChildCityList(String city,final boolean isSn){
        List<AppCitysBean> list_startcity=getStartCityList(isSn);
        for (AppCitysBean mAppCitysBean:list_startcity) {
            city=getCityName(city);
            String name=getCityName(mAppCitysBean.getName());
            if (city.equals(name)) {//如厦门等于厦门
               return mAppCitysBean.getChild_citys();
            }
        }
        return new ArrayList<ChildCitysBeanX>();
    }

    /**
     * 获取出发地的具体地址
     * @param startchildcity   具体出发地地址名字
     * @param isSn
     * @return
     */
    public static ChildCitysBeanX getStartChildCity(String startchildcity,boolean isSn){
        List<AppCitysBean> list_startcity=getStartCityList(true);
        for(AppCitysBean AppCitysBean:list_startcity){
            for(ChildCitysBeanX mChildCitysBeanX:AppCitysBean.getChild_citys()){
                startchildcity=getCityName(startchildcity);
                String name=ToCity2.getCityName(mChildCitysBeanX.getName());
                //含有该市内城市，
                if(startchildcity.equals(name)){
                    return mChildCitysBeanX;
                }
            }
        }
        return null;
    }

    /**
     * 获取目的地集合，如 厦门
     * @param startchildcity
     * @param isSn
     * @return
     */
    public static List<ToCitiesBean> getEndCityList(String startchildcity,final boolean isSn){
        List<AppCitysBean> list_startcity=getStartCityList(isSn);
        for(AppCitysBean mAppCitysBean:list_startcity){
            for(ChildCitysBeanX mChildCitysBeanX:mAppCitysBean.getChild_citys()){
                startchildcity=getCityName(startchildcity);
                String name=getCityName(mChildCitysBeanX.getName());
               if(startchildcity.equals(name)){
                   return mChildCitysBeanX.getTo_cities();
               }
            }
        }
        return new ArrayList<ToCitiesBean>();
    }

    /**
     * 获取目的地子类集合，如 厦门的思明区
     * @param startchildcity
     * @param endcity
     * @param isSn
     * @return
     */
    public static List<ChildCitysBean> getEndChildCityList(String startchildcity,String endcity,final boolean isSn){
        List<ToCitiesBean> list_endcity=getEndCityList(startchildcity, isSn);
        for(ToCitiesBean mToCitiesBean:list_endcity){
            endcity=getCityName(endcity);
            String name=getCityName(mToCitiesBean.getName());
            if(endcity.equals(name)){
                return mToCitiesBean.getChild_citys();
            }
        }
        return new ArrayList<ChildCitysBean>();
    }

    /**
     * 获取目的地子类[具体目的地地址]，如 厦门的思明区
     * @param startchildcity 具体的出发地地址，
     * @param endchildcity   具体的目的地地址
     * @param isSn
     * @return
     */
    public static ChildCitysBean getEndChildCity(String startchildcity,String endchildcity,final boolean isSn){
        List<ToCitiesBean> list_endcity=getEndCityList(startchildcity,isSn);
        for(ToCitiesBean mToCitiesBean:list_endcity){
            for(ChildCitysBean mChildCitysBean:mToCitiesBean.getChild_citys()){
                endchildcity=getCityName(endchildcity);
                String name=getCityName(mChildCitysBean.getName());
                if(endchildcity.equals(name)){
                    return mChildCitysBean;
                }
            }
        }
        return null;
    }

    /**
     * @param list_startcity
     * 过滤目的地show==1表示隐藏的
    */
    public static List<AppCitysBean> filterEndNoShow(List<AppCitysBean> list_startcity){
        for(AppCitysBean mAppCitysBean:list_startcity){
            for(ChildCitysBeanX mChildCitysBeanX:mAppCitysBean.getChild_citys()){
                for(ToCitiesBean mToCitiesBean:mChildCitysBeanX.getTo_cities()){
                    List<ChildCitysBean> list_endchildcity = mToCitiesBean.getChild_citys();
                    for(int i=list_endchildcity.size()-1;i>=0;i--){
                        //过滤
                        String ishow=list_endchildcity.get(i).getIs_show();
                        if(ishow!=null&&ishow.equals(Value.CITY_NOSHOW)){
                            list_endchildcity.remove(i);
                        }
                    }
                }
            }
        }
        return list_startcity;
    }

    /**
     * @param list_startcity
     * 过滤没有目的地的出发地子城市
     *
     */
    public static List<AppCitysBean> filterNoEnd(List<AppCitysBean> list_startcity){
        for(AppCitysBean mAppCitysBean:list_startcity){
            List<ChildCitysBeanX> list_startchildcity=mAppCitysBean.getChild_citys();
            if(list_startchildcity!=null){
                for(int i=list_startchildcity.size()-1;i>=0;i--){
                    List<ToCitiesBean> list_endcity = list_startchildcity.get(i).getTo_cities();
                    if(list_endcity==null||list_endcity.isEmpty()){
                        list_startchildcity.remove(i);
                    }
                }
            }
        }
        return list_startcity;
    }
    /**
     * @param list_startcity
     * 过滤没有出发地子城市的。
     *
     */
    public static List<AppCitysBean> filterNoStartChildCity(List<AppCitysBean> list_startcity){
        for(int i=list_startcity.size()-1;i>=0;i--){
            List<ChildCitysBeanX> list_startchildcity=list_startcity.get(i).getChild_citys();
            if(list_startchildcity==null||list_startchildcity.isEmpty()){
                list_startcity.remove(i);
            }
        }
        return list_startcity;
    }
    /**
     * @param startchildcity  出发地子类，具体出发地地址
     * @param endchildcity    目的地子类，具体目的地地址
     * @param isSn            是否是市内
     * @return
     * 获取线路id
     */
    public static String getLine(String startchildcity,String endchildcity,final boolean isSn){
        List<ToCitiesBean> list_endcity= getEndCityList(startchildcity, isSn);
        for(ToCitiesBean mToCitiesBean:list_endcity){
            for(ChildCitysBean mChildCitysBean:mToCitiesBean.getChild_citys()){
                endchildcity=getCityName(endchildcity);
                String name=getCityName(mChildCitysBean.getName());
                if(endchildcity.equals(name)) {
                    return mChildCitysBean.getLine_id();
                }
            }
        }
        return "";
    }

    /**
     * @param line_id  线路id
     * @param isSn     是否是市内
     * @return
     * 根据线路id获取电话号码
     */
    public static String getPhone(String line_id,boolean isSn){
        List<AppCitysBean> list_startcity=getStartCityList(isSn);
        for(AppCitysBean mAppCitysBean:list_startcity){
            for(ChildCitysBeanX mChildCitysBeanX:mAppCitysBean.getChild_citys()){
                for(ToCitiesBean mToCitiesBean:mChildCitysBeanX.getTo_cities()){
                    for(ChildCitysBean mChildCitysBean:mToCitiesBean.getChild_citys()){
                        if(mChildCitysBean.getLine_id().equals(line_id)){
                            return  mChildCitysBean.getIncity_phone();
                        }
                    }
                }
            }
        }
        return "";
    }

    /**
     * @param isSn
     * @return
     * 获取线路集合，，优惠劵有用
     */
    public static  List<String> getListLineId(boolean isSn){
        List<String> list_line_id=new ArrayList<String>();
        List<AppCitysBean> list_startcity=getStartCityList(isSn);
        for(AppCitysBean mAppCitysBean:list_startcity){
            for(ChildCitysBeanX mChildCitysBeanX:mAppCitysBean.getChild_citys()){
                for(ToCitiesBean mToCitiesBean:mChildCitysBeanX.getTo_cities()){
                    for(ChildCitysBean mChildCitysBean:mToCitiesBean.getChild_citys()){
                        list_line_id.add(mChildCitysBean.getLine_id());
                    }
                }
            }
        }
        return  list_line_id;
    }

    /**
     * @param isSn
     * @return
     * 获取线路哪里到哪里集合，优惠劵有用
     */
    public static List<String> getListLineName(boolean isSn){
        List<String> list_line_name=new ArrayList<String>();
        List<AppCitysBean> list_startcity=getStartCityList(isSn);
        for(AppCitysBean mAppCitysBean:list_startcity){
            for(ChildCitysBeanX mChildCitysBeanX:mAppCitysBean.getChild_citys()){
                for(ToCitiesBean mToCitiesBean:mChildCitysBeanX.getTo_cities()){
                    for(ChildCitysBean mChildCitysBean:mToCitiesBean.getChild_citys()){
                        list_line_name.add(mChildCitysBean.getLine_name());
                    }
                }
            }
        }
        return  list_line_name;
    }

    /**
     * @param startchilccity   出发地的具体地址
     * @param endchildcity     目的地的具体地址
     * @param isStart          判断是否是出发地
     * @param isSn             判断是否是市内
     * @return
     * 判断该地址是否是具体地址
     */
    public static boolean isCityDetail(String startchilccity,String endchildcity,boolean isStart,boolean isSn){
        if(isStart){
            List<AppCitysBean> list_startcity=getStartCityList(isSn);
            for(AppCitysBean AppCitysBean:list_startcity){
                for(ChildCitysBeanX mChildCitysBeanX:AppCitysBean.getChild_citys()){
                    startchilccity=getCityName(startchilccity);
                    String name=mChildCitysBeanX.getName();
                    if(startchilccity.equals(name) && mChildCitysBeanX.getIs_detail().equals(Value.CITY_DETAIL)){
                        return true;
                    }
                }
            }

        }else{
            List<ToCitiesBean> list_endcity= getEndCityList(startchilccity, isSn);
            for(ToCitiesBean mToCitiesBean:list_endcity){
                for(ChildCitysBean mChildCitysBean:mToCitiesBean.getChild_citys()){
                    endchildcity=getCityName(endchildcity);
                    String name=getCityName(mChildCitysBean.getName());
                    if(endchildcity.equals(name) && mChildCitysBean.getIs_detail().equals(Value.CITY_DETAIL)){
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * @param district  二级城市
     * @param isSn      是否是市内
     * @return
     * 判断二级城市是否存在，注意只适合用于出发地
     */
    public static boolean isDistrictExist(String district,boolean isSn){
        List<AppCitysBean> list_startcity=getStartCityList(isSn);
        for(AppCitysBean AppCitysBean:list_startcity){
            for(ChildCitysBeanX mChildCitysBeanX:AppCitysBean.getChild_citys()){
                district=getCityName(district.trim());
                String name=getCityName(mChildCitysBeanX.getName());
                if(name.equals(district)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * @param childcity
     * @param isSn
     * @return
     * 判断是否含有该城市，注意只适合用于出发地
     */
    public static boolean isContains(String childcity,boolean isSn){
            List<AppCitysBean> list_startcity=getStartCityList(isSn);
            for(AppCitysBean AppCitysBean:list_startcity){
                for(ChildCitysBeanX mChildCitysBeanX:AppCitysBean.getChild_citys()){
                    childcity=getCityName(childcity);
                    String name=getCityName(mChildCitysBeanX.getName());
                    if(childcity.equals(name)){
                        return true;
                    }
                }
            }
       return false;
    }

    /**
     * @param isSn
     * 设置出发地的城市列表
     */
    public static void setStartCityList(boolean isSn){
            //如果时候市内则不需要设置，因为市内的出发地没有城市列表
            if(isSn)return;
            List<AppCitysBean> list_startcity=getStartCityList(isSn);
            MyApplication mApplication = MyApplication.getInstance();
            mApplication.onPrepareStartCity(list_startcity, isSn);

    }

    public static void setEndCityList(Context context,String startchildcity,boolean isSn){
        List<City> allcity = new ArrayList<City>();
        List<ToCitiesBean> list_endcity=new ArrayList<>();
        if(isSn){
            String jsonStr = AppJsonFileReader.getJson(context,fileName);
            LocalCity mLocalCity = (LocalCity) new JsonBuild().getData(LocalCity.class,
                    jsonStr);
            ArrayList<LocalCity.AllCityList> allcityList=mLocalCity.getAllCityList();
            for(int i=0;i<allcityList.size();i++){
                ArrayList<LocalCity.AllCityList.CityList> cityList=allcityList.get(i).getCityList();
                //有子城市
                if(cityList!=null&&cityList.size()>0){
                    for(int num=0;num<cityList.size();num++){
                        String name=cityList.get(num).getName();
                        String province=allcityList.get(i).getName();
                        String allpy=cityList.get(num).getPinyin();
                        String AllFristPY=cityList.get(num).getShortPinyin();
                        String firstPY=AllFristPY.length()>=1?AllFristPY.charAt(0)+"":"";
                        ToCitiesBean mToCitiesBean=new ToCitiesBean();
                        mToCitiesBean.setCity(name);
                        mToCitiesBean.setName(name);
                        mToCitiesBean.setProvince(province);
                        mToCitiesBean.setIsSn(isSn);
                        ChildCitysBean mChildCitysBean=new ChildCitysBean();
                        mChildCitysBean.setCity(name);
                        mChildCitysBean.setName(name);
                        mChildCitysBean.setIsSn(isSn);
                        List<ChildCitysBean> list_endchildcity = new ArrayList<ChildCitysBean>();
                        list_endchildcity.add(mChildCitysBean);
                        mToCitiesBean.setChild_citys(list_endchildcity);
                        list_endcity.add(mToCitiesBean);
                    }
                }
                //没有子城市，则是直辖市那些
                else{
                    String name=allcityList.get(i).getName();
                    String province=allcityList.get(i).getName();
                    String allpy=allcityList.get(i).getPinyin();
                    String AllFristPY=allcityList.get(i).getShortPinyin();
                    String firstPY=AllFristPY.length()>=1?AllFristPY.charAt(0)+"":"";
                    ToCitiesBean mToCitiesBean=new ToCitiesBean();
                    mToCitiesBean.setCity(name);
                    mToCitiesBean.setName(name);
                    mToCitiesBean.setProvince(province);
                    mToCitiesBean.setIsSn(isSn);
                    ChildCitysBean mChildCitysBean=new ChildCitysBean();
                    mChildCitysBean.setCity(name);
                    mChildCitysBean.setName(name);
                    mChildCitysBean.setIsSn(isSn);
                    List<ChildCitysBean> list_endchildcity = new ArrayList<ChildCitysBean>();
                    list_endchildcity.add(mChildCitysBean);
                    mToCitiesBean.setChild_citys(list_endchildcity);
                    list_endcity.add(mToCitiesBean);
                }

            }
            MyApplication mApplication = MyApplication.getInstance();
            mApplication.onPrepareEndCity(list_endcity,isSn);
        }
        //城际
        else{
            list_endcity.addAll(getEndCityList(startchildcity, isSn));
        }
        MyApplication mApplication = MyApplication.getInstance();
        mApplication.onPrepareEndCity(list_endcity,isSn);
    }



    /**
     * 根据地址获取城市
     */
    public static String getCity(String address) {
        String city;
        if (address.contains(" ")) {
            city = address.substring(0, address.indexOf(" "));
        } else if (address.contains("\t")) {
            city = address.substring(0, address.indexOf("\t"));
        } else if (address.contains("-")) {
            city = address.substring(0, address.indexOf("-"));
        } else {
            city = address;
        }
        return city;
    }
    /**
     * 获取城市的名字，去掉市，区，县。
     */
    public static String getCityName(String city) {
        if(city!=null&&!city.equals("")){
            for(int i=0;i<ToCity2.split.length;i++){
                if((city.charAt(city.length()-1)+"").equals(ToCity2.split[i])){
                    city=city.substring(0, city.length()-1);
                    break;
                }
            }
        }
        return city;
    }
}
